<!--#include file="Ini.asp"-->
<!--#include file="../AspLib/Functions.asp"-->
<%

sREGIAO = Request("REGIAO") 
Response.expires = 0
Set Conn = Server.CreateObject("ADODB.Connection")
conn.CommandTimeout = 1500
Conn.Open sCo1, sCo2, sCo3
MDIVULGADOR=SESSION("DIVULGADOR")
SESSION("DIG_IN")=""
MSQL="select REGIAO FROM REGIAO_CEP WHERE DIVULGADOR ='"& MDIVULGADOR &"'"
'RESPONSE.WRITE MSQL
'RESPONSE.END
Set rs = Conn.Execute(MSql)			 
IF NOT RS.EOF THEN
  sREGIAO = RS("REGIAO") 
END IF

MSQL="select COD_VD FROM  DIVULGADOR_COD_DESP_VD WHERE COD_DESP ='"& MDIVULGADOR &"'"
Set rs = Conn.Execute(MSql)			 
IF NOT RS.EOF THEN
  MVD=RS("COD_VD")
  MSQL="SELECT COD_DIVUL FROM PNLD_2005_MUN WHERE COD_DIVUL='"& MVD &"' "
  Set rs = Conn.Execute(MSql)			 
  IF NOT RS.EOF THEN
     SESSION("DIG_IN")="'" & MVD & "'"
  END IF
END IF

IF SESSION("DIG_IN")="" THEN
  MSQL="select DIVULG_F1 FROM  DIVULGADOR_COD_DESP_VD WHERE COD_DESP ='"& MDIVULGADOR &"' AND DIVULG_F1>'     '"
  Set rs = Conn.Execute(MSql)			 
  'RESPONSE.WRITE MSQL
  'RESPONSE.END
  IF NOT RS.EOF THEN
    IF LEN(TRIM(RS("DIVULG_F1"))) > 0 THEN
      SESSION("DIG_IN")=TRIM(RS("DIVULG_F1"))
      MSQL="select COD_VD FROM  DIVULGADOR_COD_DESP_VD WHERE COD_DESP IN ("& SESSION("DIG_IN") &")"
      Set rs = Conn.Execute(MSql)			 
      MIN=""
      DO WHILE NOT RS.EOF
         MIN=MIN &"'" & RS("COD_VD") &"'"
         RS.MOVENEXT
         IF NOT RS.EOF THEN
           MIN=MIN & ","
         END IF
      LOOP
      SESSION("DIG_IN")=MIN 
    END IF
  ELSE
    MSQL="SELECT COD_DIVUL FROM PNLD_2005_MUN "
    IF SESSION("ESTADO")<>"" THEN
      sREGIAO = MID(SESSION("ESTADO"),2,2)
       MSQL= MSQL & " WHERE REGIAO IN ("& SESSION("ESTADO") & ")"
    END IF
    MSQL= MSQL & " GROUP BY COD_DIVUL"
    Set rs = Conn.Execute(MSql)			 
    DO WHILE NOT RS.EOF
        MIN=MIN &"'" & RS("COD_DIVUL") &"'"
        RS.MOVENEXT
        IF NOT RS.EOF THEN
           MIN=MIN & ","
        END IF
    LOOP
    SESSION("DIG_IN")=MIN 
    'RESPONSE.WRITE MSQL
    'RESPONSE.END
  END IF
END IF

MSQL="SELECT COD_DIVUL,SUM(QTD) AS QTD,SUM(VALOR) AS VALOR,SUM(QTDB) AS QTDB,SUM(VALORB) AS VALORB,SUM(QTDEDI) AS QTDEDI,SUM(VALOREDI) AS VALOREDI FROM PNLD_2005_MUN "
MSQL=MSQL & " WHERE COD_DIVUL IN ("& SESSION("DIG_IN") &") GROUP BY COD_DIVUL"
MSQL=MSQL & " ORDER BY COD_DIVUL"
'RESPONSE.WRITE MSQL
'RESPONSE.END
Set rs = Conn.Execute(MSql)			 



%>
<html>
<head>
<title><% = sTitle %></title>
<% = sBODY %>
<% = sCABECALHO %>PNLD 2005 EDITORAS <% = sFONT %></b>
  <table width='100%' border='1'<% =  sTABLE %>>
  <tr bgcolor='<% = COR_TARJA %>'>
  <td align='center'>Edi</td>
  <td align='center'>Liv</td>
  <td>Divulgador</td>
  <td align='right'>Editoras</font></td>
  <td align='right'>EBSA</font></td>
  <td align='right'>Qt%</font></td>
  </tr><%
  mcnt=0
  GQTD=0
  GQTDB=0
 Do while not rs.eof
    mcnt=mcnt+1
    GQTD=GQTD+rs("QTD")
    GQTDB=GQTDB+rs("QTDB")
    MCODD= rs("COD_DIVUL")
    MSQL="SELECT A1_NREDUZ FROM SA1010 WHERE A1_COD='"& MCODD &"' AND D_E_L_E_T_<>'*'"
    Set A1= Conn.Execute(MSql)			 
    IF NOT A1.EOF THEN
       MNOME=MID(A1("A1_NREDUZ"),1,12)
    END IF
    IF rs("QTD")=0 THEN
      M_QTD=1
    ELSE
      M_QTD=rs("QTD")
    END IF
    %><TR>
    <td align='center'><a href='pnld2005_editora_curva.asp?DIVUL=<% = MCODD %>&REGIAO=<% = SREGIAO %>'>Edi</A></td>
    <td align='center'><a href='pnld2005_livro_curva.asp?DIVUL=<% = MCODD %>&REGIAO=<% = SREGIAO %>'>Liv</A></td>
    <td><a href='pnld2005_municipio.asp?DIVULGADOR=<% = MCODD %>&REGIAO=<% = SREGIAO %>'>&nbsp;<% = MNOME %></A></td>
    <td align='right'><% = FORMATNUMBER(rs("QTD"),0) %></td>
    <td align='right'><% = FORMATNUMBER(rs("QTDB"),0) %></td>
    <td align='right'><% = FORMATNUMBER((rs("QTDB")/M_QTD)*100,1) %></td>
    </tr><%
    rs.MoveNext     	
  loop
  IF GQTD>0 THEN
	  %><tr>
	  <td colspan='3'>Total <% = MCNT %> ITENS<br></td>
	  <td align='right'><B><% = FORMATNUMBER(GQTD,0)  %></td>
	  <td align='right'><B><% = FORMATNUMBER(GQTDB,0)  %></td>
	  <td align='right'><B><% = FORMATNUMBER((GQTDB/GQTD)*100,1)  %></td>
          </tr><%
  END IF
rs.Close : Set rs = Nothing
Conn.Close : Set conn = Nothing 

%>
